﻿using JetBrains.ProjectModel;
using JetBrains.ReSharper.CodeInsight.Services.CamelTyping;
using JetBrains.ReSharper.Psi;
using JetBrains.ReSharper.Psi.Caches;
using JetBrains.ReSharper.UnitTestExplorer;
using Objectware.Silverlight.Testing.MetaData;

namespace Objectware.Silverlight.Testing.Runner.Resharper.Elements
{
	public class ClassTestElement : TestElement, ITestClass
	{
        public ClassTestElement(IUnitTestProvider provider,
								IProjectModelElement project,
								string typeName,
								string assemblyLocation)
            : base(provider, null, project, typeName)
        {
            this.AssemblyLocation = assemblyLocation;
        }

		public string AssemblyLocation { get; private set; }


		public override IDeclaredElement GetDeclaredElement()
		{
			ISolution solution = GetSolution();


			if (solution == null)
				return null;

			PsiManager manager = PsiManager.GetInstance(solution);
			DeclarationsCacheScope scope = DeclarationsCacheScope.ProjectScope(GetProject(), false);
			IDeclarationsCache cache = manager.GetDeclarationsCache(scope, true);
			return cache.GetTypeElementByCLRName(GetTypeClrName());
		}

		public override string GetKind()
		{
			return "Silverlight Test Class";
		}


		public override bool Matches(string filter, PrefixMatcher matcher)
		{
			foreach (UnitTestElementCategory category in GetCategories())
				if (matcher.IsMatch(category.Name))
					return true;

			return matcher.IsMatch(GetTypeClrName());
		}

		public override bool IsNameIdentical(TestElement element)
		{
			return this.GetTypeClrName() == element.GetTypeClrName();
		}


		#region ITestClass 
		public string Name
		{
			get { return this.GetTypeClrName(); }
		}
		#endregion
	}
}
